import global, { GameRecord, UserSkill } from "../../global";
import Storage from "../../util/storage";
import GameSkill from "../UI/gameSkill";
import Big, {Buff, Evolution, Spell } from "./big";
import Player from "./player";
import Point_ts, { Mark, MarkType } from "./point";
import Small, { Invest } from "./small";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Mgr extends cc.Component {

    //战场摄像机
    @property(cc.Camera)
    Camera:cc.Camera = null;
    //地图层
    @property(cc.Node)
    Layer_map:cc.Node = null;
    //buff层  
    @property(cc.Node)
    Layer_buff:cc.Node = null;
    //spell层  
    @property(cc.Node)
    Layer_spell:cc.Node = null;
    //键层
    @property(cc.Node)
    Layer_jian:cc.Node = null;
    //点击提示层
    @property(cc.Node)
    Layer_tip:cc.Node = null;
    //落点
    @property(cc.Prefab)
    Prefab_point:cc.Prefab = null;
    //小子
    @property(cc.Prefab)
    Prefab_small:cc.Prefab = null;
    //大子
    @property(cc.Prefab)
    Prefab_big:cc.Prefab = null;
    
    map_point:Map<number,Point_ts> = new Map();

    //1-4 玩家颜色不同
    arr_color = [cc.color(219,58,81),cc.color(56,62,153),cc.color(104,38,0),cc.color(255,0,174)];

    me_player:Player = null;
    turn_player:Player = null;  //当前回合玩家
    is_start = false;           //收到所有玩家信息和地图信息后，对局开始
    game_players:Player[] = []; //游戏玩家

    gameRecord:GameRecord = null;       //对局记录

    skill_ts:GameSkill = null;          //方便找到使用的是哪个技能
    
    onLoad () {
        global.mgr = this;
        //开启触屏移动
		this.node.on(cc.Node.EventType.TOUCH_MOVE,this.slideScreen,this);
        //如果有数据，表示在复盘，否则就开始正常对局
        if(global.is_replay){
            this.scheduleOnce(()=>{
                //展示地图
                this.build(global.temp.gameRecord.map);
                //加入玩家
                global.temp.gameRecord.players.forEach(player=>{
                    this.addPlayer(player);
                });
                this.is_start = true;
                //开始对局
                global.UI.openTurnTip("开 始 复 盘");
                global.mgr.clear();
            },1);
        }else{
           //创建对局记录
            this.gameRecord = new GameRecord();
            this.gameRecord.action = new Array();
            //发送自己的player对象
            global.client.send({method:"player",skills:global.data.userSkill,id:global.id,seat:global.seat});
        }
	}

    /**
     * 滑动屏幕
     */
    slideScreen(event:cc.Event.EventTouch){
        this.Camera.node.setPosition(this.Camera.node.getPosition().sub(event.getDelta()));
        //防止超出边界
        if(this.Camera.node.x<0){
            this.Camera.node.x = 0;
        }else if(this.Camera.node.x>4000){
            this.Camera.node.x = 4000;
        }
        if(this.Camera.node.y<0){
            this.Camera.node.y = 0;
        }else if(this.Camera.node.y>4000){
            this.Camera.node.y = 4000;
        }
    }
    

    /**
     * 收到信息进行分析
     */
    bh = 0;             //收到消息
    receive(data:any){
        if(!this.is_start){     //对局还未开始，此时还不进行计数
            switch(data.method){
                case "build": this.build(data);break;                             //地图搭建
                case "player": this.addPlayer(data);break;                        //玩家信息
                case "start": this.startGame();break;                             //对局开始
            };
            return;
        }
        this.bh++;                       //下次收到消息，编号应该是这个
        switch(data.method){
            case "turn": this.turn(data.id);this.refreshView();break;                    //回合切换
            case "smallDown": this.smallDown(data);this.refreshView();break;             //由于后台记录，这里还是单独再写一次，其实属于常规操作
            case "operation": 
                this.operation(data);
                this.scheduleOnce(()=>{this.refreshView();},0);
                break;                   //常规操作 
            case "unlockSkill":
                this.unlockSkill(data);
                break;
            case "gameOver": this.gameOver(data.type);break;                    //不落小子，游戏结束
            case "skill":this.skill(data);this.scheduleOnce(()=>{this.refreshView();},0);break;     //普通技能
            case "skillOnlyMe":this.skillOnlyMe(data);break;        //不刷新页面的技能
            case "admitDefeat":                                     //认输
            case "dropLine":this.admitDefeat(data.id);break;        //掉线
            case "dissolution":global.client = null;break;          //对局解散
            default : console.error("错误数据：",data);
        };
        if(!global.is_replay){
            //如果当前是我的回合，监听归位
            if(this.turn_player.is_me&&this.me_player.small){
                this.me_player.homing();
            }
            //记录操作
            this.gameRecord.action.push(data);
            //对局超时
            if(this.bh>1000){
                this.gameOver("soLang");
            }
        }
    }

    /**
     * 地图搭建,对局人数
     */
    is_build = false;
    type = -1;
    build(data:any){
        let types = data.map;
        this.type = data.type
        let index = 0;
        for(let type of types){
            let node = cc.instantiate(this.Prefab_point);
            node.getComponent(Point_ts).init(type,index);
            this.Layer_map.addChild(node);
            this.map_point.set(index,node.getComponent(Point_ts));
            index++;
        }
        this.is_build = true;
        if(!global.is_replay){
            //检查是否准备好
            this.checkReady();
            //记录地图
            this.gameRecord.map = data;  
        }
    }

    /**
     * 接收玩家信息
     */
    player_num = 0;
    addPlayer(data:any){
        let userSkill = data.skills;
        let id = data.id;
        let seat = data.seat;
        
        if(global.is_replay){
            let player = new Player();
            player.color = this.arr_color[this.player_num];
            player.init(userSkill,id);
            this.game_players.push(player);
            this.player_num++;
            player.is_me = true;
        }else{
            let player = new Player();
            player.color = this.arr_color[seat];
            player.init(userSkill,id);
            this.game_players.push(player);
            //后手补偿
            if(seat>0){
                player.changePower(1);
            }
            //记录玩家信息
            this.gameRecord.players[seat] = data;
            this.player_num++;
            //检查是否准备好
            this.checkReady();  
        }
    }

    /**
     * 检查是否具备开始条件
     */
    checkReady(){
        if(this.player_num==this.type){
            if(this.is_build){
                global.client.send({method:"ready"});
            }
        }
    }

    /**
     * 开始游戏
     */
    startGame(){
        this.is_start = true; 
        //对局数+1
        global.data.num_match++;
        Storage.set("data",global.data);
    }

    /**
     * 回合结束换人
     */
    turn(id:number){
        if(global.is_replay){
            //上一回合的玩家有些事情要做
            this.turn_player&&this.turn_player.arr_big.forEach(big_ts=>{
                big_ts.dayOver();
            });
            this.turn_player&&this.turn_player.arr_big.forEach(big_ts=>{
                big_ts.dayOverOver();
            });
            //玩家做出相应动作
            this.game_players.forEach(player=>{
                player.turnReady();
            })
            this.turn_player = this.getPlayer(id);
            this.turn_player.turnMe();
        }else{
            if(id==global.id && this.me_player.is_surrender){
                global.UI.turnOver();
                return;
            }
            //如果不是自己的回合，取消全部事件监听
            this.map_point.forEach((element)=>{
                element.offAll();
            });
            this.me_player.offAll();
            //重新倒计时
            global.UI.timeDown();
            //上一回合的玩家有些事情要做
            this.turn_player&&this.turn_player.arr_big.forEach(big_ts=>{
                big_ts.dayOver();
            });
            this.turn_player&&this.turn_player.arr_big.forEach(big_ts=>{
                big_ts.dayOverOver();
            });
            //玩家做出相应动作
            this.game_players.forEach(player=>{
                player.turnReady();
            })
            this.turn_player = this.getPlayer(id);
            if(this.turn_player != null && !this.turn_player.is_surrender){
                this.turn_player.turnMe();
            }else{
                this.game_players.forEach(player=>{
                    console.log("player.id: ",player.id);
                })
                console.log("this.turn_player--null: id");
            }
            
        }
    }

    /**
     * 游戏结束
     */
    is_gameOver:boolean = false;
    gameOver(type:string,winner:number = 0){
        if(this.is_gameOver||global.is_replay){
            return;
        }
        this.is_gameOver = true;
        if(type=="backStage" && this.game_players.length==2){
            cc.director.loadScene("index");
            global.persist.tip("对方切后台导致掉线");
            //对局数-1
            global.data.num_match++;
            Storage.set("data",global.data);
        }else if(type=="noSmall"){
            cc.director.loadScene("index");
            global.persist.tip("不落小子，游戏结束");
            //对局数-1
            global.data.num_match++;
            Storage.set("data",global.data);
        }else if(type=="logicError"){
            global.persist.tip("逻辑异常，游戏结束");
            //对局数-1
            global.data.num_match++;
            Storage.set("data",global.data);
            global.frame.yesPreserve();
        }else if(type=="normal"){
            global.frame.openFrameSettleAccounts(winner);
        }else if(type=="soLong"){
            global.persist.tip("对局超时，平局结束");
            global.frame.openFrameSettleAccounts(-1);
        }
    }
    
    /**
     * 落小子
     */
    smallDown(data:any) {
        let player = this.getPlayer(data.id);
        player.smallDown(data.index);
    }

    /**
     * 玩家行动
     */
    operation(data:any) {
        let player = this.getPlayer(data.id);
        player.operation(data);
    }

    /**
     * 技能使用
     */
    skill(data:any){
        switch(data.skill){
            case "csjj":this.csjj(data.index);  this.openTurnTip("常胜将军");break;
            case "xysz":this.xysz(data.index);  this.openTurnTip("骁勇善战");break;
            case "pzjp":this.pzjp(data.index);  this.openTurnTip("炮转狙炮");break;
            case "gzcg":this.gzcg(data.index);  this.openTurnTip("鬼转超鬼");break;
            case "dysf":this.dysf(data);        this.openTurnTip("地狱杀伐");break;
            case "qxbs":this.qxbs(data.index);  this.openTurnTip("潜行捕手");break;
            case "sxzh":this.sxzh(data.index1,data.index2); this.openTurnTip("属性置换");break;
            case "jmxj":this.jmxj(data.index);  this.openTurnTip("绝命献祭");break;
            case "bdsl":this.bdsl(data.id,data.index);      this.openTurnTip("冰冻手雷");break;
            case "ssds":this.ssds(data.id);     this.openTurnTip("审时度势");break;
            case "wycb":this.wycb(data.index);  this.openTurnTip("瘟疫传播");break;
            case "smwz":this.smwz(data.index);  this.openTurnTip("沙漠王子");break;
            case "cfpl":this.cfpl(data.index);  this.openTurnTip("乘风破浪");break;
            case "gbwc":this.gbwc(data);        this.openTurnTip("诡变无常");break;
            case "qszh":this.qszh(data.index,data.id);      this.openTurnTip("敲山震虎");break;
            case "bsdf":this.bsdf(data.index1,data.index2); this.openTurnTip("搬山大法");break;
            case "jqhh":this.jqhh(data.id,data.index);      this.openTurnTip("九曲黄河");break;
            case "ffcb":this.ffcb(data.id,data.index);      this.openTurnTip("佛法慈悲");break;
            case "mwsl":this.mwsl(data.id,data.index);      this.openTurnTip("迷雾森林");break;
            case "ssbx":this.ssbx(data.id);     this.openTurnTip("生生不息");break;
            case "tltx":this.tltx(data.index);  this.openTurnTip("桃李天下");break;
            case "elfs":this.elfs(data.index);  this.openTurnTip("恶灵附身");break;
            case "jsbm":this.jsbm(data.index);     this.openTurnTip("金身不灭");break;
            case "sbtj":this.sbtj(data.id,data.index);  this.openTurnTip("神兵天降");break;
            case "fxbs":this.fxbs(data.id,data.index);  this.openTurnTip("风雪冰霜");break;
            case "sxmn":this.sxmn(data.index);  this.openTurnTip("嗜血蛮牛");break;
            case "tysd":this.tysd(data.index);  this.openTurnTip("天御圣灯");break;
            case "wedt":this.wedt(data.index);  this.openTurnTip("万厄毒体");break;
            case "jtsk":this.jtsk(data.id,data.index);  break;
            case "cpzj":this.cpzj(data.index);  this.openTurnTip("拆炮专家");break;
            case "zgds":this.zgds(data.index);  this.openTurnTip("捉鬼大师");break;
            default:console.error("出现未知技能");
        }
    }

    /**
     * 特殊技能使用,不刷新视野
     */
    skillOnlyMe(data:any){
        switch(data.skill){
            case "mhsg":this.openTurnTip("明魂搜根");break;
            case "tycz":this.openTurnTip("天眼传真");break;
            default:console.error("出现未知技能");
        }
    }

    openTurnTip(s:string){
        if(global.is_replay){
            global.UI.openTurnTip(s);
        }
    }

    /**
     * 刷新视野
     */
    refreshView(){
        //死亡或者认输，自动切换到观战视角
        if(!this.me_player||this.me_player.is_surrender||global.is_omniscience){
            this.map_point.forEach(point_ts=>{
                point_ts.display(true);
            })
            return;
        }
        //如果自己没有小子，则地图全亮
        if(!this.me_player.small){
            return;
        }
        //先清空全部视野
        this.map_point.forEach(element => {
            element.is_view = false;
            element.display();
        });
        //所有己方小子大子提供视野
        let arr = [];
        if(this.me_player.small){
            arr = this.me_player.small.getViewArr();
        }
        this.me_player.arr_big.forEach((big_ts)=>{
            arr.push(...big_ts.getViewArr());
        });
        let set = new Set(arr);
        set.forEach(index=>{
            let point_ts = this.map_point.get(index);
            //是否有迷雾
            let is_fog = false;
            point_ts.arr_mark.forEach(mark=>{
                if(mark.type==MarkType.FOG&&mark.id!=this.me_player.id){
                    is_fog = true;
                }
            });
            if(!is_fog){
                point_ts.is_view = true;
                point_ts.display();
            }else{
                let big = point_ts.node.getChildByName("大子");
                if(big&&big.getComponent(Big).player.id==this.me_player.id){
                    point_ts.is_view = true;
                    point_ts.display();
                }
            }
            //所有小子提供视野
            this.game_players.forEach(player=>{
                player.showSmallView();
            })
        });
    }

    /**
     * 取消落点的事件监听
     */
    pointOffAll(){
        for(let node of this.Layer_map.children){
            let point_ts = node.getComponent(Point_ts);
            point_ts.offAll();
        }
    }

    /**
     * 获取全部视野内的落点
     */
    getArrPointView(){
        let arr = new Array<Point_ts>();
        this.map_point.forEach(point_ts=>{
            if(point_ts.is_view){
                arr.push(point_ts);
            }
        });
        return arr;
    }


    /**
     * 使用技能前，清空全部事件监听
     */
    clear(){
        this.map_point.forEach(point_ts=>{
            point_ts.offAll();
            let big = point_ts.node.getChildByName("大子");
            if(big){
                big.getComponent(Big).closeOn();;
            }
            let small = point_ts.node.getChildByName("小子");
            if(small){
                small.getComponent(Small).closeOn();;
            }
        });
    }

    /**
     * 有玩家认输
     */
    admitDefeat(id:number){
        if(this.is_gameOver){
            return
        }
        //玩家认输，清除战场上关于他的全部信息
        let player = this.getPlayer(id);
        if(player.is_surrender){        //手动认输后，掉线会再执行一次，这里它返回
            return;
        }
        player.is_surrender = true;
        player.small&&player.small.killed();    //自杀
        this.map_point.forEach(point_ts=>{
            point_ts.arr_mark.forEach(mark=>{
                if(mark.id==id){
                    point_ts.arr_mark.delete(mark);
                }
            });
        });
        //如果只有自己一个玩家，直接获胜
        for(let i=0;i<this.game_players.length;i++){
            let p = this.game_players[i];
            if(player == p){
                this.game_players.splice(i,1);
            }
        }
        if(this.game_players.length==1){
            this.gameOver("normal",global.id);
            return;
        }
        //认输后必须的操作
        if(player.is_me){   
            global.persist.tip("自动转入观战");
            if(this.turn_player == player){
                global.UI.turnOver();
            }
        }else{
            global.persist.tip("一名玩家死亡");
        }
    }

    /******************************************以下是技能**************************************** */
   
    /**
     * 常胜将军
     */
    csjj(index:number){
        let point_ts = global.mgr.map_point.get(index);
        let big_ts = point_ts.node.getChildByName("大子").getComponent(Big);
        big_ts.arr_spell.add(Spell.CSJJ); 
        //有视野才可以看到
        if(global.is_replay||point_ts.is_view){
            global.effect.playBigSpell(point_ts.node.position);
        }
    }

    /**
     * 骁勇善战
     */
    xysz(index:number){
        let point_ts = global.mgr.map_point.get(index);
        let big_ts = point_ts.node.getChildByName("大子").getComponent(Big);
        big_ts.arr_spell.add(Spell.XYSZ); 
        //有视野才可以看到
        if(global.is_replay||point_ts.is_view){
            global.effect.playBigSpell(point_ts.node.position);
        }
    }

    /**
     * 炮转狙炮
     */
    pzjp(index:number){
        let point_ts = global.mgr.map_point.get(index);
        let big_ts = point_ts.node.getChildByName("大子").getComponent(Big);
        big_ts.arr_spell.add(Spell.PZJP);
        //有视野才可以看到
        if(global.is_replay||point_ts.is_view){
            global.effect.playBigSpell(point_ts.node.position);
        }
    }

    /**
     * 鬼转超鬼
     */
    gzcg(index:number){
        let point_ts = global.mgr.map_point.get(index);
        let big_ts = point_ts.node.getChildByName("大子").getComponent(Big);
        big_ts.arr_spell.add(Spell.GZCG);
        //有视野才可以看到
        if(global.is_replay||point_ts.is_view){
            global.effect.playBigSpell(big_ts.node.parent.position);
        }
    }

    /**
     * 地狱杀伐
     */
    dysf(data:any){
        let player = this.getPlayer(data.id);
        let zf_node = global.effect.playDYSF(this.map_point.get(data.index),player);
        let arr = global.getAround2(data.index);
        arr.push(data.index);
        arr.forEach(index=>{
            let point_ts = this.map_point.get(index);
            let mark = new Mark(data.id,MarkType.BLOODY);
            mark.zf_node = zf_node;
            point_ts.arr_mark.add(mark);
        });
    }

    /**
     * 根据id返回player
     */
    getPlayer(id:number){
        for(let player of this.game_players){
            if(player.id == id){
                return player;
            }
        }
    }

    /**
     * 潜行捕手
     */
    qxbs(index:number){
        let point_ts = global.mgr.map_point.get(index);
        let big_ts = point_ts.node.getChildByName("大子").getComponent(Big);
        big_ts.arr_spell.add(Spell.QXBS); 
        //有视野才可以看到
        if(global.is_replay||point_ts.is_view){
            global.effect.playBigSpell(big_ts.node.parent.position);
        }
    }

    /**
     * 属性置换
     */
    sxzh(index1:number,index2:number){
        let point1 = global.mgr.map_point.get(index1).node;
        let point2 = global.mgr.map_point.get(index2).node;
        let big1 = point1.getChildByName("大子");
        let big2 = point2.getChildByName("大子");
        let big1_ts = big1.getComponent(Big);
        let big2_ts = big2.getComponent(Big);
        //符咒交换
        let temp_speel = big1_ts.arr_spell;
        big1_ts.arr_spell = big2_ts.arr_spell;
        big2_ts.arr_spell = temp_speel;
        //buff交换
        let temp_buff = big1_ts.arr_buff;
        big1_ts.arr_buff = big2_ts.arr_buff;
        big2_ts.arr_buff = temp_buff;
    }

    /**
     * 绝命献祭
     */
    jmxj(index:number){
        let point_ts = global.mgr.map_point.get(index);
        let big_ts = point_ts.node.getChildByName("大子").getComponent(Big);
        let player = big_ts.player;
        let num_before = player.arr_big.size;
        big_ts.die();
        let num_after = player.arr_big.size;
        let cha = num_before - num_after;
        player.changeAction(cha);
    }

    /**
     * 过度分裂专用
     */
    checkPoint(point_ts:Point_ts){
        if(point_ts.real_type!=0){
            return false;
        }
        if(point_ts.node.getChildByName("大子")){   //空地不可能有隐身大子
            return false;
        }
        if(point_ts.node.getChildByName("小子")){
            return false;
        }
        return true;
    }

    /**
     * 冰冻手雷
     */
    bdsl(id:number,index:number){
        //播放冰冻特效
        let point_ts = this.map_point.get(index);
        if(global.is_replay||point_ts.is_view){
            global.effect.playBDSL(point_ts.node.position);
        }
        //延时，等动画播放完了再动
        this.scheduleOnce(()=>{
        let arr = global.getAround(index);
        arr.push(index);
        arr.forEach(number=>{
            let point_ts = this.map_point.get(number);
            let big = point_ts.node.getChildByName("大子"); 
            if(big){
                let big_ts = big.getComponent(Big);
                if(big_ts.player.id!=id&&big_ts.evolution==Evolution.Null){
                    big_ts.arr_buff.add(Buff.ice)
                }
            }
        });
        },.5);
        
    }

    /**
     * 审时度势
     */
    ssds(id:number){
        let player = this.getPlayer(id);
        player.changeAction(-4);
        player.small.arr_invest.add(Invest.SSDS);
    }

    /**
     * 瘟疫传播
     */
    wycb(index:number){
        let point_ts = this.map_point.get(index);
        let big = point_ts.node.getChildByName("大子");
        if(big){
            big.getComponent(Big).arr_buff.add(Buff.virus);
        }
        if(global.is_replay||point_ts.is_view){
            global.effect.playWYCB(point_ts.node.position);
        }
    }

    /**
     * 沙漠王子
     */
    smwz(index:number){
        let point_ts = global.mgr.map_point.get(index);
        point_ts.node.getChildByName("大子").getComponent(Big).arr_spell.add(Spell.SMWZ); ;
        //有视野才可以看到
        if(global.is_replay||point_ts.is_view){
            global.effect.playBigSpell(point_ts.node.position);
        }
    }

    /**
     * 乘风破浪
     */
    cfpl(index:number){
        let point_ts = global.mgr.map_point.get(index);
        point_ts.node.getChildByName("大子").getComponent(Big).arr_spell.add(Spell.CFPL);
        //有视野才可以看到
        if(global.is_replay||point_ts.is_view){
            global.effect.playBigSpell(point_ts.node.position);
        }
    }

    /**
     * 诡变无常
     */
    gbwc(data:any){
        let player = this.getPlayer(data.id);
        let zf_node = global.effect.playGBWC(this.map_point.get(data.index),player);
        let arr = global.getAround2(data.index);
        arr.push(data.index);
        arr.forEach(index=>{
            let point_ts = this.map_point.get(index);
            let mark = new Mark(data.id,MarkType.VARIABLE);
            mark.zf_node = zf_node;
            point_ts.arr_mark.add(mark);
        });
    }

    /**
     * 敲山震虎
     */
    qszh(index:number,id:number){
        let point_ts = this.map_point.get(index);
        //看看是不是已经振过了
        let flag = false;
        point_ts.arr_mark.forEach(mark=>{
            if(mark.id==id&&mark.type==MarkType.QUAKE){
                flag = true;
            }
        });
        if(flag){
            return;
        }
        //开始震
        point_ts.arr_mark.add(new Mark(id,MarkType.QUAKE));
        if(global.is_replay||point_ts.is_view){
            let action = cc.tween().by(.1, {x: 30}).by(.1, {x: -30});
            cc.tween(point_ts.node).repeat(3,action).start();
        }
        let arr = global.getAround(index);
        arr.forEach(number=>{
            let point_ts = this.map_point.get(number);
            let big = point_ts.node.getChildByName("大子");
            if(big){
                let big_ts = big.getComponent(Big);
                if(big_ts.player.id!=id&&big_ts.evolution==Evolution.Null){
                    big_ts.arr_buff.add(Buff.fear);
                }
            }
            if(point_ts.real_type==1){
                this.qszh(point_ts.index,id);
            }   
        });
    }

    /**
     * 搬山大法
     */
    bsdf(index1:number,index2:number){
        let point_ts1 = this.map_point.get(index1);
        let point_ts2 = this.map_point.get(index2);
        point_ts1.real_type = 0;
        point_ts2.real_type = 1;
    }

    /**
     * 九曲黄河
     */
    jqhh(id:number,index:number){
        let arr = global.getAround2(index);
        arr.push(index);
        arr.forEach(number=>{
            let point_ts = this.map_point.get(number);
            point_ts.arr_mark.add(new Mark(id,MarkType.MAZE));
        });
        global.effect.playJQHH(this.map_point.get(index),this.getPlayer(id));
    }

    /**
     * 佛法慈悲
     */
    ffcb(id:number,index:number){
        let player = this.getPlayer(id);
        let zf_node = global.effect.playFFCB(this.map_point.get(index),player);
        let arr = global.getAround2(index);
        arr.push(index);
        arr.forEach(index=>{
            let point_ts = this.map_point.get(index);
            let mark = new Mark(id,MarkType.MERCY);
            mark.zf_node = zf_node;
            point_ts.arr_mark.add(mark);
        });
    }

    /**
     * 迷雾森林
     */
    mwsl(id:number,index:number){
        let player = this.getPlayer(id);
        let zf_node = global.effect.playMWSL(this.map_point.get(index),player);
        let arr = global.getAround2(index);
        arr.push(index);
        arr.forEach(index=>{
            let point_ts = this.map_point.get(index);
            let mark = new Mark(id,MarkType.FOG);
            mark.zf_node = zf_node;
            point_ts.arr_mark.add(mark);
        });
    }

    /**
     * 风雪冰霜
     */
    fxbs(id:number,index:number){
        let player = this.getPlayer(id);
        let zf_node = global.effect.playFXBS(this.map_point.get(index),player);
        let arr = global.getAround2(index);
        arr.push(index);
        arr.forEach(index=>{
            let point_ts = this.map_point.get(index);
            let mark = new Mark(id,MarkType.SNOW);
            mark.zf_node = zf_node;
            point_ts.arr_mark.add(mark);
        });
    }

    /**
     * 生生不息
     */
    ssbx(id:number){
        let player = this.getPlayer(id);
        player.changeAction(2);
    }

    /**
     * 桃李天下
     */
    tltx(index:number){
        let point_ts = this.map_point.get(index);
        point_ts.node.getChildByName("大子").getComponent(Big).arr_spell.add(Spell.TLTX);
        //有视野才可以看到
        if(global.is_replay||point_ts.is_view){
            global.effect.playBigSpell(point_ts.node.position);
        }
    }

    /**
     * 恶灵附身
     */
    elfs(index:number){
        let point_ts = this.map_point.get(index);
        point_ts.node.getChildByName("大子").getComponent(Big).arr_spell.add(Spell.ELFS);
        //有视野才可以看到
        if(global.is_replay||point_ts.is_view){
            global.effect.playBigSpell(point_ts.node.position);
        }
    }

    /**
     * 金身不灭
     */
    jsbm(index:number){
        let point_ts = this.map_point.get(index);
        point_ts.node.getChildByName("大子").getComponent(Big).arr_buff.add(Buff.gold);
        //有视野才可以看到
        if(global.is_replay||point_ts.is_view){
            global.effect.playBigSpell(point_ts.node.position);
        }
    }

    /**
     * 神兵天降
     */
    sbtj(id:number,index:number){
        let player = this.getPlayer(id);
        let point_ts = this.map_point.get(index);
        //出大
        let big = cc.instantiate(this.Prefab_big);
        let big_ts = big.getComponent(Big);
        big_ts.Sprite.node.color = player.color;
        big_ts.player= player;
        big_ts.Jian.color = player.color;
        point_ts.node.addChild(big);
        big_ts.init(point_ts);
        big_ts.arr_buff.add(Buff.ice);
        big_ts.is_clickable = false;
        player.arr_big.add(big_ts)
        //播放音效
        if(global.is_replay||point_ts.is_view){
            global.music_ts.playEffect("Audio_born");
        }
    }

    /**
     * 嗜血蛮牛
     */
    sxmn(index:number){
        let point_ts = this.map_point.get(index);
        point_ts.node.getChildByName("大子").getComponent(Big).arr_spell.add(Spell.SXMN);
        //有视野才可以看到
        if(global.is_replay||point_ts.is_view){
            global.effect.playBigSpell(point_ts.node.position);
        }
    }

    /**
     * 天御圣灯
     */
    tysd(index:number){
        let point_ts = this.map_point.get(index);
        point_ts.node.getChildByName("大子").getComponent(Big).arr_spell.add(Spell.TYSD);
        //有视野才可以看到
        if(global.is_replay||point_ts.is_view){
            global.effect.playBigSpell(point_ts.node.position);
        }
    }

    /**
     * 万厄毒体
     */
    wedt(index:number){
        let point_ts = this.map_point.get(index);
        point_ts.node.getChildByName("大子").getComponent(Big).arr_spell.add(Spell.WEDT);
        //有视野才可以看到
        if(global.is_replay||point_ts.is_view){
            global.effect.playBigSpell(point_ts.node.position);
        }
    }

    /**
     * 狡兔三窟
     */
    jtsk(id:number,index:number){
        let point_ts = this.map_point.get(index);
        let player = this.getPlayer(id);
        player.small.node.setParent(point_ts.node);
        player.small.Node_tip.setPosition(player.small.node.parent.position);
        player.arr_big.forEach(big_ts=>{
            big_ts.showJian();
        });
        this.openTurnTip("狡兔三窟");
        if(id==this.me_player.id){
            global.UI.turnOver();
        }
    }

    /**
     * 拆炮专家
     */
     cpzj(index:number){
        let point_ts = this.map_point.get(index);
        point_ts.node.getChildByName("大子").getComponent(Big).arr_spell.add(Spell.CPZJ);
        //有视野才可以看到
        if(global.is_replay||point_ts.is_view){
            global.effect.playBigSpell(point_ts.node.position);
        }
    }

    /**
     * 捉鬼大师
     */
     zgds(index:number){
        let point_ts = this.map_point.get(index);
        point_ts.node.getChildByName("大子").getComponent(Big).arr_spell.add(Spell.ZGDS);
        //有视野才可以看到
        if(global.is_replay||point_ts.is_view){
            global.effect.playBigSpell(point_ts.node.position);
        }
    }

    /**
     * 是否可以播放音效
     */
     canPlayEffect(point_ts:Point_ts,player:Player){
        //是自己，直接播放
        if(player.id==global.id){
            return true;
        }
        //没视野直接不考虑
        if(!point_ts.is_view){
            return false;
        }
        let big_ts = point_ts.node.getChildByName("大子").getComponent(Big);     //应该是有这个大子的
        if(big_ts.is_hide){
            return false;
        }
        return true;
    }


    /**
     * 技能解锁
     */
    unlockSkill(data:any){
        let player = this.getPlayer(data.id);
        player.num_unlockSkills++;
    }


    /*********************************下面的复盘的功能******************************************************** */

    protected start(): void {
        if(global.is_replay){
            this.scheduleOnce(()=>{
                this.clickAutoNext();
            },1);
        }
    }

    /**
     * 复盘系统的下一步
     */
    index_replay = 0;
    next(){
        if(this.index_replay==global.temp.gameRecord.action.length){
            global.mgr.clear();
            this.is_pause = true;
            global.frame.openFrameReplay();
            return;
        }
        this.receive(global.temp.gameRecord.action[this.index_replay++]);
        global.mgr.clear();
    }

    /**
     * 自动播放
     */
    is_pause = false;
    is_auto = false;
    clickAutoNext(){
        this.is_pause = false;
        if(!this.is_auto){
            this.is_auto = true;
            this.autoNext();
        }else{
            return;
        }
    }
    autoNext(){
        this.scheduleOnce(()=>{
            if(!this.is_pause){
                this.next();
                this.autoNext();
            }else{
               this.is_auto = false; 
            }
        },.5);
    }

    /**
     * 暂停
     */
    pause(){
        this.is_pause = true;
    }

    /**
     * 切换视角 三人局，
     */
    index_view = 0;
    switchView(){
        this.index_view++;
        this.me_player = this.game_players[this.index_view];
        if(!this.me_player||this.me_player.is_surrender){
            this.index_view=-1;
        }
        this.refreshView();
    } 
    
}
